#include"preCmpH.h"
#include"UILabel.h"
#include"UIFontManager.h"
#include"UIManager.h"

BEGIN_NAMESPACE(ui_manager)
BEGIN_NAMESPACE(ui_controls)

UILabel::UILabel()
: text_()
, font_()
, textChanged_(false)
{
}
UILabel::UILabel(const UILabel& label)
: text_(label.text_)
, font_(label.font_)
{
}
UILabel& UILabel::operator= (const UILabel& label)
{
	if(this == &label){
		return *this;
	}
	text_ = label.text_;
	font_ = label.font_;
	return *this;
}
UILabel::~UILabel(void)
{
}

void UILabel::BoundBy( UIManager& manager, bool is_component )
{
	static int counter = 0;
	UIControl::BoundBy(manager,is_component);
	if(background_ == null){
		background_ = &manager.getUIRenderer().GenerateImage("uilabel_default" + counter, size_.getWidth(), size_.getHeight(), true, 0);
		counter++;
	}
	if(!font_.IsValid()){
		font_ = manager.getUIFontManager().GetDefault();
	}
	
}
void UILabel::Render(UIPoint parentPosistion)
{
	if(text_.length() == 0)
	{
		return;
	}
	if(textChanged_ ){
		mcInterface_.getUIRenderer().DrawTextToImage(
			text_,
			font_,
			*background_);
		textChanged_ = false;
	}
	mcInterface_.getUIRenderer().DrawImage(
		pos_.getX() + parentPosistion.getX(),
		pos_.getY() + parentPosistion.getY(),
		background_->GetWidth(),
		background_->GetHeight(),
		*background_);
}
void UILabel::HandleEvent(ui_events::UIEvent ui_event)
{
	switch (ui_event.getMsgFlag())
	{
	case ui_events::UIEvent::FLAG_KEYBOARD:
		break;
	case ui_events::UIEvent::FLAG_MOUSE:
		break;
	case ui_events::UIEvent::FLAG_RENDER:
		{
			Render(ui_event.getRenderParentPosition());
		}
	default:
		break;
	}
}

END_NAMESPACE // UI controls
END_NAMESPACE // UI manager